<template>
  <div class="register-container">
    <!-- 注册内容 -->
    <div class="register">
      <h3>注册新用户
        <span class="go">我有账号，去 <router-link to="/login" >登陆</router-link>
        </span>
      </h3>

      <!-- handleSubmit校验表单的方法 -->
      <!-- 
      <ValidationProvider name="Last Name" rules="required|alpha" v-slot="{ errors }">
        <input v-model="lastName" type="text">
        <span>{{ errors[0] }}</span>
      </ValidationProvider>

      <button type="submit">Submit</button>
       -->
      <ValidationObserver v-slot="{ handleSubmit }">
      <form @submit.prevent="handleSubmit(onSubmit)">
        <!-- 手机号 -->
        <ValidationProvider class="content" rules="phone|phoneRequired" tag="div" mode="lazy" v-slot="{ errors }">
          <label>手机号:</label>
          <input v-model="user.phone" type="text" placeholder="请输入你的手机号" >
          <span class="error-msg">{{ errors[0] }}</span>
        </ValidationProvider>

        <!-- 验证码 -->
        <ValidationProvider class="content" rules="code|codeRequired" tag="div" mode="lazy" v-slot="{ errors }">
          <label>验证码:</label>
          <input v-model="user.code" type="text" placeholder="请输入验证码" >
          <span class="sendCode" v-if="isSendCode" @click="sendCode"> 获取验证码</span>
          <span v-if="!isSendCode">还剩({{time}})s</span>
          <span class="error-msg">{{ errors[0] }}</span>
        </ValidationProvider>
        <!-- 密码 -->
        <ValidationProvider class="content" rules="password|passwordRequired" tag="div" mode="lazy" v-slot="{ errors }">
          <label>登录密码:</label>
          <input v-model="user.password" type="text" placeholder="请输入你的登录密码" >
          <span class="error-msg">{{ errors[0] }}</span>
        </ValidationProvider>
        <!-- 再次密码 -->
        <ValidationProvider class="content" :rules="`rePassword:${user.password}|rePasswordRequired`" tag="div" mode="lazy" v-slot="{ errors }">
          <label>确认密码:</label>
          <input v-model="user.rePassword" type="text" placeholder="请输入确认密码" >
          <span class="error-msg">{{ errors[0] }}</span>
        </ValidationProvider>
       
        <!-- 协议框 -->
        <ValidationProvider class="controls" rules="verify" tag="div" mode="lazy" v-slot="{ errors }">
          <label >
            <input name="m1" type="checkbox" v-model="user.isVerify">
            <span> 同意协议并注册<a>《尚品汇用户协议》</a></span>
          </label>
          <span class="error-msg">{{ errors[0] }}</span>
        </ValidationProvider>
        <div class="btn">
          <button :class="{isverify:!user.isVerify}" @click="asd">完成注册</button>
        </div>
      </form>
      </ValidationObserver>
    </div>

    <!-- 底部 -->
    <div class="copyright">
      <ul>
        <li>关于我们</li>
        <li>联系我们</li>
        <li>联系客服</li>
        <li>商家入驻</li>
        <li>营销中心</li>
        <li>手机尚品汇</li>
        <li>销售联盟</li>
        <li>尚品汇社区</li>
      </ul>
      <div class="address">地址：北京市昌平区宏福科技园综合楼6层</div>
      <div class="beian">京ICP备19006430号
      </div>
    </div>
  </div>
</template>

<script>
import {ValidationProvider,ValidationObserver,extend} from "vee-validate"
import {required} from "vee-validate/dist/rules"
import {sendCode,reqRegister} from "@/api"
import "@/utils/rules"

//检察验证码
extend("code", {
  validate(val) {
    return /^[0-9]{6}$/.test(val);
  },
  message: "验证码不合法",
});
extend("codeRequired", {//必填
  ...required,
  message: "请输入验证码",
});


//验证第二次输入的密码
extend("rePassword", {
  validate(val, { password }) {
    // val就是表单项的值
    // 返回true，代表校验通过
    return val === password;
  },
  message: "两次密码输入不一致",
  params: ["password"],
});
extend("rePasswordRequired", {
  ...required,
  message: "请输入确认密码",
});

//验证是否勾选 协议
extend("verify", {
  ...required,
  validate(val) {
    return val;
  },
  message: "请同意用户协议",
});

  export default {
    name: 'Register',
    data:()=>({
      user:{
        phone: "",
        code: "",
        password: "",
        rePassword: "",
        isVerify: false,
      },
      time: 60,
      isSendCode: true,
    }),
    computed:{
      disabled(){
        return /^1[3-9][0-9]{9}$/.test(this.user.phone)
      }
    },
    methods: {
       async onSubmit () {
        const { phone, code, password } = this.user;
        try { //注册成功
          await reqRegister({ phone, code, password })
          this.$router.push("/login");
        } catch (error) {
          // 注册失败
          alert(error)
        } 
      },
      async sendCode(){
        //先验证手机是否符合要求
        if(!this.disabled) return
        this.isSendCode = false
        const timer = setInterval(()=>{
          if(this.time <= 1) {
            clearInterval(timer)
            this.time=60
            this.isSendCode = true
          }
          this.time--
        },1000)
        //发送验证码
        const code = await sendCode(this.user.phone)
        console.log(code);
      },
      asd(e){
        if(!this.user.isVerify) {
          e.preventDefault()
        }}
    },
    components:{
      ValidationObserver,
      ValidationProvider,
    }
  }
</script>
 
<style lang="less" scoped>
  .register-container {
    .register {
      width: 1200px;
      height: 445px;
      border: 1px solid rgb(223, 223, 223);
      margin: 0 auto;

      h3 {
        background: #ececec;
        margin: 0;
        padding: 6px 15px;
        color: #333;
        border-bottom: 1px solid #dfdfdf;
        font-size: 20.04px;
        line-height: 30.06px;

        span {
          font-size: 14px;
          float: right;

          a {
            color: #e1251b;
          }
        }
      }

      div:nth-of-type(1) {
        margin-top: 40px;
      }

      .content {
        padding-left: 390px;
        margin-bottom: 18px;
        position: relative;

        label {
          font-size: 14px;
          width: 96px;
          text-align: right;
          display: inline-block;
        }

        input {
          width: 270px;
          height: 38px;
          padding-left: 8px;
          box-sizing: border-box;
          margin-left: 5px;
          outline: none;
          border: 1px solid #999;
        }

        img {
          vertical-align: sub;
        }

        .error-msg {
          position: absolute;
          top: 100%;
          left: 495px;
          color: red;
        }
      }
      .sendCode{
        margin-left: 5px;
        padding: 7px 2px;
        font-size: 14px;
        border: 1px solid #bbb;
        border-radius:2px;
        background-color: #eee;
        cursor: pointer;
        &:hover{
          background-color: rgb(214, 208, 208);
        }
      }
      .controls {
        text-align: center;
        position: relative;

        input {
          margin-bottom: 1px;
          vertical-align: middle;
        }

        .error-msg {
          position: absolute;
          top: 100%;
          left: 495px;
          color: red;
        }
      }

      .btn {
        text-align: center;
        line-height: 36px;
        margin: 17px 0 0 55px;
        
        button {
          outline: none;
          width: 270px;
          height: 36px;
          background: #e1251b;
          border:0;
          border-radius:3px;
          color: #fff ;
          display: inline-block;
          font-size: 16px;
          &:hover{
            background-color: rgb(252, 81, 81);
            cursor: pointer;
          }
          &.isverify{
            cursor: not-allowed;
            background-color: rgb(255, 142, 142);
          }
        }
      }
    }

    .copyright {
      width: 1200px;
      margin: 0 auto;
      text-align: center;
      line-height: 24px;

      ul {
        li {
          display: inline-block;
          border-right: 1px solid #e4e4e4;
          padding: 0 20px;
          margin: 15px 0;
        }
      }
    }
  }
</style>